/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.activity.comp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import org.niclab.vanbus.R;
import org.niclab.vanbus.activity.controller.BusStopController;
import org.niclab.vanbus.activity.controller.ReminderController;
import org.niclab.vanbus.database.ApplicationDataBase;
import org.niclab.vanbus.database.ReminderDAO;
import org.niclab.vanbus.model.BusRoute;
import org.niclab.vanbus.model.BusStop;
import org.niclab.vanbus.model.StartStopReminder;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteConstraintException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class StartStopReminderDialog {
	
	private static final String LOG_TAG="StartStopReminder";
	
	
	

	private BusStop stop;
	private BusRoute route;
	
	private AlertDialog dialog;
	private Context context;
	private Activity activity;
	
	private View layout;
	private TextView futureTimesText;
	private TextView distanceMsgText;
	private TextView durationMsgText;
	private TextView dateText;
	private TimePicker timePicker;
	private Button backButton;
	private Button forwardButton;
	
	//private TimeStamp time;
	private Calendar date;
	private String title;
	private String futureTimes;
	private String distanceMsg;
	private String durationMsg;
	private ReminderController reminderController;

	private StartStopReminder reminder;
	


	public StartStopReminderDialog(Activity parentAct){
		this(parentAct, null);
		
	}
	
	public StartStopReminderDialog(Activity parentAct, StartStopReminder reminder) {

		this.activity = parentAct;
	
		this.context = parentAct.getApplicationContext();
		
		reminderController= new ReminderController(context);
		this.reminder = reminder;
		
		
		layout=activity.getLayoutInflater().inflate(R.layout.reminder_start_stop_dialog, null);
	
		
		futureTimesText = (TextView) layout.findViewById(R.id.reminder_start_stop_future_times);
		distanceMsgText = (TextView) layout.findViewById(R.id.reminder_start_stop_direction_distance);
		durationMsgText = (TextView) layout.findViewById(R.id.reminder_start_stop_direction_time);
		dateText = (TextView) layout.findViewById(R.id.reminder_start_stop_date);
		
		timePicker = (TimePicker) layout.findViewById(R.id.reminder_start_stop_timepicker);
		timePicker.setOnTimeChangedListener(new OnTimeChangedListener(){

			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay,
					int minute) {
				date.set(Calendar.HOUR_OF_DAY, hourOfDay);
				date.set(Calendar.MINUTE,minute);
				
			}
			
		});
		
		
		backButton = (Button) layout.findViewById(R.id.reminder_start_stop_date_back);
		backButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				goToYesterday();
				
			}
			
		});
		forwardButton = (Button) layout.findViewById(R.id.reminder_start_stop_date_forward);
		forwardButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				goToTomorrow();
				
			}
			
		});
		
		
		
	}


	public AlertDialog createDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle("Set a Reminder");
		builder.setView(layout);
		
		builder.setPositiveButton("Set a Reminder", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				try{
					setUpReminder();
				}catch(SQLiteConstraintException e){
					AlertDialog.Builder builder = new AlertDialog.Builder(activity);
					builder.setMessage("Sorry, there is already a start stop reminder for this stop.");
					
					builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							
						}
					});
					
					builder.create().show();
				}
			}

			
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		dialog=builder.create();
		return dialog;
	}
	
	public void prepareDialog(Dialog dialog){
		Log.d(LOG_TAG,"title:"+title);
		dialog.setTitle(title);
		futureTimesText.setText(this.futureTimes);
		distanceMsgText.setText(this.distanceMsg);
		durationMsgText.setText(this.durationMsg);
		setTimePickerTime();
		setDateText();
		
	}
	public AlertDialog getDialog(){
		return dialog;
	}
	
	public void setTitle(String message){
		this.title = message;
		
	}
	
	public void setFutureTimes(String times){
		this.futureTimes = times;
	}
	
	public void setWalkingDistanceMessage(String msg){
		this.distanceMsg = msg;
	}
	public void setWalkingTimeMessage(String msg){
		this.durationMsg = msg;
	}
	
	public void setDate(Calendar date){
		this.date =date;
						
	}
	
	public Calendar getDate(){
		return date;
	}
	
	private void setTimePickerTime(){
		timePicker.invalidate();
		timePicker.setCurrentHour(date.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(date.get(Calendar.MINUTE));
		
	}
	
	
	private void setDateText(){
		if(date==null)
			date = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy");
		String str = dateFormat.format(date.getTime());
		dateText.setText(str);
	}
	
	
	
	private void goToYesterday(){
		date.add(Calendar.DAY_OF_MONTH, -1);
		setDateText();
		
	}
	
	private void goToTomorrow(){
		date.add(Calendar.DAY_OF_MONTH,1);
		setDateText();
		
	}
	
	public void setReminder(StartStopReminder reminder){
		this.reminder = reminder;
	}

	protected void setUpReminder() throws SQLiteConstraintException{
		reminder.setAlarmTime(getDate().getTime());
		try{
			reminderController.setStartStopReminder(reminder);
		}catch(SQLiteConstraintException e){
			throw e;
		}
	}
	
	
	

}
